package com.zzh.lib.core.loader;


import android.content.Context;
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.zzh.lib.core.HLibrary;
import com.zzh.lib.core.utils.MimeTypeUtils;

import androidx.annotation.NonNull;
import androidx.loader.content.CursorLoader;

import static android.provider.MediaStore.MediaColumns.MIME_TYPE;


/**
 * Created by ZZH on 2023/2/1.
 *
 * @date: 2023/2/1 17:35
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.core.loader
 * Available columns [_id, title, _data, _display_name, album, album_artist, _size, date_added, duration, author, mime_type]
 */
public class HAudioLoader extends CursorLoader {
    public HAudioLoader(@NonNull Context context) {
        super(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            setProjection(new String[]{MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.ALBUM_ARTIST,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media.DATE_ADDED,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.AUTHOR,
                    MediaStore.Audio.Media.ARTIST,
                    MIME_TYPE});
        } else {
            setProjection(new String[]{MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.ALBUM,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media.DATE_ADDED,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.ARTIST,
                    MIME_TYPE});
        }


        setUri(MediaStore.Files.getContentUri("external"));
        setSortOrder(MediaStore.Audio.Media.DATE_ADDED + " DESC");

        if (TextUtils.isEmpty(HLibrary.getAudioSelection())) {
            String defaultSelection = "mime_type =? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=? OR mime_type=?";
            setSelection(defaultSelection);
        } else {
            setSelection(HLibrary.getAudioSelection());
        }
        if (HLibrary.getAudioSelectionArgs() == null) {
            String[] selectionArgs = new String[]{MimeTypeUtils.getMimeType("amr"), MimeTypeUtils.getMimeType("mp3"), MimeTypeUtils.getMimeType("aac"), "audio/mp3", "audio/flac", "audio/m4a"};
            setSelectionArgs(selectionArgs);
        } else {
            setSelectionArgs(HLibrary.getAudioSelectionArgs());
        }

    }
}
